<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        #banner{
            width: 1226px;
            height: 460px;
            margin: 100px auto;
            position: relative;
        }

        #banner img{
            position: absolute;
            transition: all 1s;
        }

        #prev, #next{
            width: 41px;
            height: 69px;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
        }

        #prev{
            background: url('./img2/icon-slides.png') -83px 0px;
            left: 0%;
        }

        #prev:hover{
            background-position: 0 0;
            cursor: pointer;
        }

        #next{
            background: url('./img2/icon-slides.png') -126px 0px;
            right: 0%;
        }

        #next:hover{
            background-position: -42px 0;
            cursor: pointer;
        }

        #list{
            position: absolute;
            right: 30px;
            bottom: 10px;
        }

        #list ul li{
            width: 6px;
            height: 6px;
            background: rgba(0,0,0,.4);
            border: hsla(0,0%,100%,.3) 2px solid;
            list-style: none;
            float: left;
            margin-right: 5px;
            border-radius: 100%;
        }

        #list ul li:hover{
            background: hsla(0,0%,100%,.4);
            border-color: rgba(0,0,0,.4);
            cursor: pointer;
        }

    </style>
</head>
<body>
    
    <div id="banner">
        <img src="./img2/a.jpg" width="1226">
        <img src="./img2/b.webp" width="1226">
        <img src="./img2/c.webp" width="1226">
        <img src="./img2/d.webp" width="1226">
        <img src="./img2/e.webp" width="1226">

        <div id="prev"></div>
        <div id="next"></div>
        <div id="list">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>

    <script>
        var banner = document.getElementById('banner');
        var img = banner.getElementsByTagName('img');
        var prev = document.getElementById('prev');
        var next = document.getElementById('next');
        var li = banner.getElementsByTagName('li');
        var index = 0;
        var timer;

        // 轮播显示
            run(); // 第一次第一张

            timer = setInterval(run, 3000);
            function run(){
                if(index >= 5){
                    index = 0;
                }

                showImg(index); 
                index++;
            }

        // 显示图片
        function showImg(n){
            // 将所有图片进行透明
            for(var i = 0; i < img.length; i++){
                img[i].style.opacity = 0;
                li[i].style.backgroundColor = 'rgba(0,0,0,.4)';
                li[i].style.borderColor = 'hsla(0,0%,100%,.3)';

            }


            img[n].style.opacity = 1;
            li[n].style.backgroundColor = 'hsla(0,0%,100%,.4)';
            li[n].style.borderColor = 'rgba(0,0,0,.4)';
        }

        // 下一张
        next.onclick = function(){
            clearInterval(timer);

            index = index >= img.length ? 0 : index;

            showImg(index);
            run();
            timer = clearInterval(run, 3000);
        }

        prev.onclick = function(){
            clearInterval(timer);

            index =  index - 1 < 0 ? img.length - 1 : index - 1 ;
            showImg(index);
            timer = setInterval(run, 3000);
        }

        // 小圆圈
        for(var i = 0; i < li.length; i++){
            (function(i){
                li[i].onclick = function(){
                    clearInterval(timer);
                    showImg(i);
                    index = i; 
                    timer = setInterval(run, 3000)
                }
            })(i);
        }

    </script>


</body>
</html>